package com.guoxue.action.common;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import com.guoxue.domain.BookConfigVo;
import com.guoxue.domain.DistrictVo;
import com.guoxue.domain.LevelConfigVo;
import com.guoxue.domain.PlayerProgressVo;
import com.guoxue.domain.TapesRecordsVo;
import com.guoxue.domain.TapesShareVo;
import com.guoxue.domain.TotalInfoVo;
import com.guoxue.service.IDistrictService;
import com.guoxue.service.ILevelConfigService;
import com.guoxue.service.IPlayerProgressService;
import com.guoxue.service.ITapesRecordsService;
import com.guoxue.service.ITapesShareService;
import com.guoxue.service.ITotalInfoService;
import com.guoxue.util.LoadConfigProperties;
@Component
/**
 * 模块化用户信息的处理接口，主要包括：
 * 1.getTotalInfo获取关注、被关注、被收听的计数
 * 2.getLevelInfo获取当前用户等级进度信息
 * 3.getDistrict获取当前用户区域信息
 * 4.getTapesArray获取当前用户上传录音信息
 */
public class ModularUser {
	@Resource
	private ITotalInfoService totalInfoService;
	@Resource
	private ILevelConfigService levelConfigService;
	@Resource
	private IPlayerProgressService playerProgressService;
	@Resource
	private ITapesRecordsService tapesService;
	@Resource
	private ITapesShareService shareService;
	@Resource
	private IDistrictService districtService;
	/**
	 * 获取关注、被关注、被收听的计数
	 * @param uid需要查询的用户ID
	 * @return TotalInfoVo
	 * @author kanghongbo
	 */
	public TotalInfoVo getTotalInfo(int uid){	
		TotalInfoVo total = totalInfoService.getInfoByUid(uid);
		if (total == null) {
			total = new TotalInfoVo();
			total.setUid(uid);
			total.setFollow(0);
			total.setBeFollowed(0);
			total.setListened(0);
			totalInfoService.saveOrUpateInfo(total);
		} 
		return total;
	}
	/**
	 * 获取当前用户等级进度信息
	 * @param uid需要查询的用户ID
	 * @return list:PlayerProgressVo,LevelConfigVo(当前等级),LevelConfigVo(下一级所需经验)
	 * @author kanghongbo
	 */
	public List getLevelInfo(int uid){	
		PlayerProgressVo player = playerProgressService.getPlayerProgressByUid(uid);// 
		int exp = 0;
		if (player == null) {
			player = new PlayerProgressVo();
			player.setUid(uid);
			player.setExp(0);
		} 
		LevelConfigVo level = levelConfigService.getLevelByExp(exp);// 获取当前等级
		LevelConfigVo nextlevel = levelConfigService.getNextLevelByExp(exp);// 获取对应下一级所需经验
		List list= new ArrayList();
		list.add(player);
		list.add(level);
		list.add(nextlevel);
		return list;
	}
	/**
	 * 获取当前用户所在区域
	 * @param uid需要查询的用户ID
	 * @return DistrictVo
	 * @author kanghongbo
	 */
	public DistrictVo getDistrict(int dstId){
		DistrictVo dst = new DistrictVo();
		if(dstId!=0){
			dst = districtService.getDistrictByKey(dstId);
		}
		return dst;
	}
	/**
	 * 获取当前用户上传录音信息
	 * @param basePath 当前访问路径
	 * @param uid需要查询的用户ID
	 * @return tapesArray
	 * @author kanghongbo
	 */
	public JSONArray getTapesArray(String basePath,int uid){
		// 获取用户上传录音信息
		List<TapesRecordsVo> tapes = tapesService.findByUidAndStatus(uid, 0);
		JSONArray tapesArray = new JSONArray();
		if (tapes.size() != 0) {// 若没有返回null
			for (TapesRecordsVo t : tapes) {// 获取对应录音点击量等数据
				JSONObject tapesInfo = new JSONObject();
				TapesShareVo share = shareService.getTapesShareByTid(t.getId(), 0);
				if (share == null){
					share = new TapesShareVo();
					share.setTapesId(t.getId());
					share.setCommentCount(0);
					share.setShareCount(0);
					share.setTotalGifts(0);
					share.setTotalListend(0);
					shareService.saveTapesShare(share);
				}	
				//TODO: 从静态资源获取数据 do
				BookConfigVo book = LoadConfigProperties.bookConfigMap.get(Integer.valueOf(t.getFlag()));
				tapesInfo.put("commentCount", share.getCommentCount());
				tapesInfo.put("gifts", share.getTotalGifts());
				tapesInfo.put("listened", share.getTotalListend());
				tapesInfo.put("shareCount", share.getShareCount());
				tapesInfo.put("tapeId", t.getId());
				tapesInfo.put("top", t.getTop());
				tapesInfo.put("size", t.getSize());
				tapesInfo.put("tapesName", t.getTapes());
				tapesInfo.put("commitTime", t.getCommitTime());
				tapesInfo.put("chapterId", t.getFlag());
				tapesInfo.put("chapterName", t.getChapter());
				tapesInfo.put("belongBook", book.getBookName());
				tapesInfo.put("compent", isEmpty(t.getUserComment())?null:t.getUserComment());
				tapesInfo.put("shareUrl", basePath);
				tapesArray.add(tapesInfo);
			}
			
		}
		return tapesArray;
	}
	
	/**
	 * 判断是否为空字符串或null
	 * @param message
	 * 			判断字符串是否为空
	 * @return
	 */
	public Boolean isEmpty(String message){
		if (message==null||"".equals(message)){
			return true;
		}else {
			return false;
		}
	}
}
